import { ConfigProvider } from 'antd';
import { useEffect, useMemo, useRef, useState } from 'react';
import Pagination from '@inbiz/basic/Pagination';
import { GlobalRegistry } from '@inbiz/core';
import { getMessage, TextWidget } from '@inbiz/react';
// import {getLogPagingList,getSearchLogList,editLogSwitch,getLogSwitch} from '../service'
import ButtonsHeader from "./ButtonsHeader";
import TableList from "./TableList";
import { observer } from '@formily/react'
import { GetDisableWord, GetExtendWord, GetSynonymWord, GetTokenizerAsync } from '../servers';

interface IProps {
    activeTab: string;
    GetPage: any;
}


const Word = (props: IProps) => {
    const { activeTab, GetPage } = props;


    // 选中的行
    const [selectLine, setSelectLine] = useState<any>([]);

    // 查询参数
    const [search, setSearch] = useState("");

    // 页码
    const [pageIndex, setPageIndex] = useState(1);

    // 页数
    const [pageSize, setPageSize] = useState(parseInt(GetPage));
    

    const tableRef = useRef<any>();

useEffect(()=>{
    setPageSize(parseInt(GetPage))
},[GetPage])


    const totalinfo = useMemo(() => {
        return getMessage('WordSegmentationManagement.totalinfo')
    }, [GlobalRegistry.getDesignerLanguage()])

    // 存储接口数据
    const [tableList, setTableList] = useState([]);

    // 数据总数
    const [dataTotal, setDataTotal] = useState(0);

    const getTableList = (index?: number, ActiveTab?: string) => {
        let _activeTab = ActiveTab || activeTab;
        let params = {
            PageIndex: index || pageIndex ,
            PageSize: pageSize,
            KeyWord: search ? search : '',
        };
        switch (_activeTab) {
            // 同义词数据
            case 'synonym':
                GetSynonymWord(params)
                    .then((res: any) => {
                        let result = res?.Data.Items;
                        let result1 = res?.Data.Total;
                        if (res?.Code == 0 && res?.Data) {
                            setTableList(result);
                            setDataTotal(result1);
                        } else {
                            setTableList([]);
                            setDataTotal(0);
                        }
                    })
                    .catch((err: any) => {
                        console.log(err);
                    });
                break;
            // 扩展词数据
            case 'extendedWord':
                GetExtendWord(params)
                    .then((res: any) => {
                        let result = res?.Data.Items;
                        let result1 = res?.Data.Total;
                        if (res?.Code == 0 && res?.Data) {
                            setTableList(result);
                            setDataTotal(result1);
                        } else {
                            setTableList([]);
                            setDataTotal(0);
                        }
                    })
                    .catch((err: any) => {
                        console.log(err);
                    });
                break;
            // 停用词数据
            case 'stopWord':
                GetDisableWord(params)
                    .then((res: any) => {
                        let result = res?.Data.Items;
                        let result1 = res?.Data.Total;
                        if (res?.Code == 0 && res?.Data) {
                            setTableList(result);
                            setDataTotal(result1);
                        } else {
                            setTableList([]);
                            setDataTotal(0);
                        }
                    })
                    .catch((err: any) => {
                        console.log(err);
                    });
                break;
            // 正则数据
            case 'regularWordSegmentationConfiguration':
                GetTokenizerAsync(params)
                    .then((res: any) => {
                        let result = res?.Data.Items;
                        let result1 = res?.Data.Total;
                        if (res?.Code == 0 && res?.Data) {
                            setTableList(result);
                            setDataTotal(result1);
                        } else {
                            setTableList([]);
                            setDataTotal(0);
                        }
                    })
                    .catch((err: any) => {
                        console.log(err);
                    });
                break;
            default:
                break;
        }
    };
    useEffect(()=>{
        getTableList(pageIndex,activeTab)
    },[pageIndex,pageSize, search, activeTab])




    return (
        <div>
                <div className="word-segmentation-management-top">
                  <ButtonsHeader
                    selectLine={selectLine}
                    setSelectLine={setSelectLine}
                    setSearch={setSearch}
                    search={search}
                    tableRef={tableRef}
                    {...props}
                  />
                </div>
                <div className="word-segmentation-management-content">
                  <TableList
                    ref={tableRef}
                    setSelectLine={setSelectLine}
                    search={search}
                    setSearch={setSearch}
                    getTableList={getTableList}
                    tableList={tableList}
                    setPageIndex={setPageIndex}
                    setPageSize={setPageSize}
                    pageIndex={pageIndex}
                    pageSize={pageSize}
                    {...props}
                  />
                </div>
            <Pagination
                pageRefresh={true}
                refreshData={() => {
                    getTableList();
                }}
                pagination={{
                    current: pageIndex,
                    pageSize: pageSize,
                    total: dataTotal,
                    showSizeChanger: true,
                    pageSizeOptions: [10, 20, 30, 50, 100],
                    showLessItems: true,
                    // @ts-ignore
                    showTotal: true,
                }}
                onPageChange={({ current, pageSize }) => {
                    setPageIndex(current);
                    setPageSize(pageSize);
                }}
            />
        </div >
    )
}

export default observer(Word);